from inc import *
def Register(request):
    u_data = UserCore()
    register_t = loader.get_template('_register.html')
    c = Context({"REQUEST":request.REQUEST})
    return HttpResponse(register_t.render(c))

def AddService(request):
    form_t = loader.get_template('_addservice.html')
    c = Context({"REQUEST":request.REQUEST})
    return HttpResponse(form_t.render(c))

def ModifyService(request):
    form_t = loader.get_template('_mdyservice.html')
    c = Context({"REQUEST":request.REQUEST})
    return HttpResponse(form_t.render(c))

def AddGirl(request,sname):
    service = GetService(sname)
    gallery_info = None
    dic = {"REQUEST":request.REQUEST,"SERVICE":sname}
    if service:
      gnode = etree.parse(CONFIG.SERVICES_PATH + sname+'/config.xml')
      gallery = Gallery.InitGalleryConfig(gnode.getroot())
      gallery_info = gallery.BasicInfo()
      dic['GALLERY_INFO'] = gallery_info
    form_t = loader.get_template('_addgirl.html')
    c = Context(dic)
    return HttpResponse(form_t.render(c))

def AddPost(request,sname):
    form_t = loader.get_template('_addpost.html')
    c = Context({"REQUEST":request.REQUEST,"SERVICE":sname})
    return HttpResponse(form_t.render(c))

def AddRoster(request,sname,day):
    form_t = loader.get_template('_addroster.html')
    c = Context({"DAY":int(day),"SERVICE":sname})
    return HttpResponse(form_t.render(c))

def ModifyTimeTable(request):
    form_t = loader.get_template('_mdytimetable.html')
    c = Context({})
    return HttpResponse(form_t.render(c))


def Login(request):
    login_t = loader.get_template('_login.html')
    c = Context({"REQUEST":request.REQUEST})
    return HttpResponse(login_t.render(c))

def GetGallery(request,sname):
    try:
      data = ServiceCore.objects.get(name = sname)
      if (data.activate == True):
        fd = os.open(CONFIG.SERVICES_PATH + sname+'/config.xml',os.O_RDWR)
        gnode = etree.parse(CONFIG.SERVICES_PATH + sname+'/config.xml')
        gallery = Gallery.InitGalleryConfig(gnode.getroot())
        return HttpResponse(gallery.XML(),mimetype = "text/xml")
      else:
        return HttpResponse("<FAIL>Service not activated</FAIL>") 
    except ServiceCore.DoesNotExist:
        return HttpResponse("<FAIL>Service not existed</FAIL>")

def AddGallery(request,sname):
    addgallery_t = loader.get_template('_addgallery.html')
    c = Context({"REQUEST":request.REQUEST})
    return HttpResponse(addgallery_t.render(c))

def AddResource(request,sname,respath):
    addgallery_t = None
    if(request.REQUEST.has_key('quick')):
      addgallery_t = loader.get_template('image_add_quick.html')
    else:
      addgallery_t = loader.get_template('image_add.html')
    c = Context({"SERVICE":sname,"RESPATH":respath})
    return HttpResponse(addgallery_t.render(c))

def ModifyResource(request,sname,respath):
    addgallery_t = None
    if(request.REQUEST.has_key('quick')):
      addgallery_t = loader.get_template('image_add_quick.html')
    else:
      addgallery_t = loader.get_template('image_add.html')
    c = Context({"SERVICE":sname,"RESPATH":respath,'MODIFY':'STRICT'})
    return HttpResponse(addgallery_t.render(c))
